﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using YXShop.Common;
using System.Collections.Generic;

namespace YXShop.Web.admin.store
{
    public partial class contact_view : System.Web.UI.Page
    {
        YXShop.BLL.Store.StoreCenter storeCenterBll = new YXShop.BLL.Store.StoreCenter();
        YXShop.BLL.Store.Store storeBLL = new BLL.Store.Store();
        protected void Page_Load(object sender, EventArgs e)
        {
            YXShop.Common.PromptInfo.Popedom("014004001");

            if(!IsPostBack)
            {
                YXShop.Model.Admin.AdminInfo admin = YXShop.Common.AdministrorManager.Get();
                List<YXShop.Model.Store.StoreCenter> centerContact = storeCenterBll.GetListShopByWhere(" and iscenter=1");
                if (centerContact.Count > 0)
                {
                    lblContact.Text = centerContact[0].Contact;
                    lblMobilePhone.Text = centerContact[0].Mobilephone;
                    lblPhone.Text = centerContact[0].Phone;
                    lblFax.Text = centerContact[0].Fax;
                    lblAddress.Text = centerContact[0].Address;
                    lblZipCode.Text = centerContact[0].Zipcode;
                    if (admin.AdminType == 1)
                    {
                        hlEditCenterContact.NavigateUrl = "storecenter_contact_edit.aspx?iscenter=1&isedit=1";
                        hlEditCenterContact.Visible = true;
                    }
                    else
                    {
                        hlEditCenterContact.Visible = false;
                    } 
                }
                string storeCenterWhere = " and iscenter=0";
                YXShop.BLL.Admin.Administrators adminBLL = new BLL.Admin.Administrators();
                if (admin.AdminType == 1)
                    storeCenterWhere += "";
                else if (admin.AdminType == 2)
                {
                    storeCenterWhere += " and adminid=" + admin.AdminId.ToString();
                }
                else if (admin.AdminType == 3)
                {
                    List<YXShop.Model.Store.StoreCenter> storecenters = adminBLL.GetStoreCenterOfAdmin_Store(admin.AdminId);
                    List<int> ids=new List<int> ();
                    foreach(YXShop.Model.Store.StoreCenter s in storecenters)
                    {
                        ids.Add(s.Sid.Value);
                    }
                    string tempWhere = ChangeHope.Common.Util.GetWhereInClause(ids, "sid");
                    storeCenterWhere += tempWhere;
                }
                this.lblStoreCenterList.Text = GetStoreCenterList(storeCenterWhere);

                string storeWhere = "";
                if (admin.AdminType == 1)
                    storeWhere += "";
                else if (admin.AdminType == 2)
                {
                    List<YXShop.Model.Store.Store> stores = adminBLL.GetStoreCenterAdmin_Store(admin.AdminId);
                    List<int> ids = new List<int>();
                    foreach (YXShop.Model.Store.Store s in stores)
                    {
                        ids.Add(s.Sid.Value);
                    }
                    string tempWhere = ChangeHope.Common.Util.GetWhereInClause(ids, "sid");
                    storeCenterWhere += tempWhere;
                }
                else if (admin.AdminType == 3)
                {
                    storeCenterWhere += " and adminid=" + admin.AdminId;
                }
                this.lblStoreList.Text = GetStoreList(storeWhere);

            }
        }

        #region 绑定
        private string GetStoreCenterList(string where)
        {
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            ChangeHope.DataBase.DataByPage dataPage = storeCenterBll.GetList(where);
            table.AddHeadCol("", "");
            table.AddHeadCol("","销售中心名称");
            table.AddHeadCol("","销售中心类型");
            table.AddHeadCol("","用户名称");
            table.AddHeadCol("","开始时间");
            table.AddHeadCol("","结束时间");
            //table.AddHeadCol("","审核状态");
            table.AddHeadCol("","操作");
            table.AddRow();
            if (dataPage.DataReader != null)
            {
                int curpage = ChangeHope.WebPage.PageRequest.GetInt("pageindex");
                if (curpage < 0)
                {
                    curpage = 1;
                }
                int count = 0;
                YXShop.Common.SysParameter sp=new YXShop.Common.SysParameter();

                YXShop.Model.Admin.AdminInfo admin = YXShop.Common.AdministrorManager.Get();
                while (dataPage.DataReader.Read())
                {                   
                    count++;
                    string No = (15 * (curpage - 1) + count).ToString();
                    table.AddCol(dataPage.DataReader["sid"].ToString());
                    table.AddCol(dataPage.DataReader["storecentername"].ToString());
                    table.AddCol(ValuePair.GetStoreType(Convert.ToInt32(dataPage.DataReader["storecentertype"])));
                    table.AddCol(GetAdminUserName(Convert.ToInt32(dataPage.DataReader["adminid"])));
                    table.AddCol(dataPage.DataReader["startdate"].ToString());
                    table.AddCol(dataPage.DataReader["enddate"].ToString());
                    string strUrl = "";
                    if (admin.AdminType == 1 || admin.AdminType == 2)
                    {
                        strUrl = YXShop.TemplateAction.Common.UrlStr("storecenter_contact_edit", "id=" + dataPage.DataReader["sid"].ToString()+"&isedit=1", "admin/store/");
                    }
                    else
                    {
                        strUrl = YXShop.TemplateAction.Common.UrlStr("storecenter_contact_edit", "id=" + dataPage.DataReader["sid"].ToString(), "admin/store/");
                    }
                    table.AddCol("<a href=\""+strUrl+"\">查看/修改</a>");
                    table.AddRow();
                }
            }
            string view = table.GetTable() + dataPage.PageToolBar;
            dataPage.Dispose();
            dataPage = null;
            return view;
        }
        private string GetStoreList(string where)
        {
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            ChangeHope.DataBase.DataByPage dataPage = storeBLL.GetList(where);
            table.AddHeadCol("", "");
            table.AddHeadCol("", "门店名称");
            table.AddHeadCol("", "门店类型");
            table.AddHeadCol("", "用户名称");
            table.AddHeadCol("", "开始时间");
            table.AddHeadCol("", "结束时间");
            //table.AddHeadCol("","审核状态");
            table.AddHeadCol("", "操作");
            table.AddRow();
            if (dataPage.DataReader != null)
            {
                int curpage = ChangeHope.WebPage.PageRequest.GetInt("pageindex");
                if (curpage < 0)
                {
                    curpage = 1;
                }
                int count = 0;
                YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
                YXShop.Model.Admin.AdminInfo admin = YXShop.Common.AdministrorManager.Get();
                while (dataPage.DataReader.Read())
                {
                    count++;
                    string No = (15 * (curpage - 1) + count).ToString();
                    table.AddCol(dataPage.DataReader["sid"].ToString());
                    table.AddCol(dataPage.DataReader["storename"].ToString());
                    table.AddCol(ValuePair.GetStoreType(Convert.ToInt32(dataPage.DataReader["storetype"])));
                    table.AddCol(GetAdminUserName(Convert.ToInt32(dataPage.DataReader["adminid"])));
                    table.AddCol(dataPage.DataReader["startdate"].ToString());
                    table.AddCol(dataPage.DataReader["enddate"].ToString());
                    string strUrl = "";
                    if (admin.AdminType == 1 || admin.AdminType == 3)
                    {
                        strUrl = YXShop.TemplateAction.Common.UrlStr("store_contact_edit", "id=" + dataPage.DataReader["sid"].ToString() + "&isedit=1", "admin/store/");
                    }
                    else
                    {
                        strUrl = YXShop.TemplateAction.Common.UrlStr("store_contact_edit", "id=" + dataPage.DataReader["sid"].ToString(), "admin/store/");
                    }
                    table.AddCol("<a href=\"" + strUrl + "\">查看/修改</a>"); table.AddRow();
                }
            }
            string view = table.GetTable() + dataPage.PageToolBar;
            dataPage.Dispose();
            dataPage = null;
            return view;
        }
        #endregion

        #region  获取数据
        /// <summary>
        /// 用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        private string GetAdminUserName(int adminuserId)
        {
            YXShop.BLL.Admin.Administrators adminuserBll = new YXShop.BLL.Admin.Administrators();
            YXShop.Model.Admin.Administrators adminuser = adminuserBll.GetModel(adminuserId);
            return adminuser.Name;

        }

        #endregion


    }
}
